home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / ARGV.ASM < prev    next >
Assembly Source File  |  1991-10-24  |  3KB  |  165 lines

  1. ;
  2.         extrn    PSP:word
  3. ;
  4. StdGrp        group    stdlib,stddata
  5. stddata        segment    para public 'sldata'
  6. stddata        ends
  7. ;
  8. stdlib        segment    para public 'slcode'
  9.         assume    cs:stdgrp
  10. ;
  11.         extrn    sl_malloc:far
  12. ;
  13. ; Argv-        Returns a string containing a specified command line
  14. ;        parameter.
  15. ;
  16. ; inputs:
  17. ;
  18. ;    AX-    Contains the number of the parameter you wish to obtain.
  19. ;    PSP-    Global variable which must point at the PSP.
  20. ;
  21. ; Outputs:
  22. ;
  23. ;    ES:DI-    Points at the newly allocated string on the heap which contains
  24. ;        the command line parameter.
  25. ;
  26. ;
  27. cr        equ    13
  28. ;
  29. ;
  30.         public    sl_Argv
  31. ;
  32. sl_Argv        proc    far
  33.         push    ds
  34.         push    si
  35.         push    ax
  36.         push    cx
  37. ;
  38.         mov    si, seg    PSP
  39.         mov    ds, si
  40.         mov    ds, ds:PSP
  41. ;
  42. ; Skip to the first parameter
  43. ;
  44.         mov    si, 80h            ;Pointer to start of cmd line-1
  45. CntLoop:    inc    si            ;Move on to next char.
  46.         cmp    byte ptr [si], ' '    ;Skip all spaces here.
  47.         je    CntLoop
  48. ;
  49.         mov    cl, [si]
  50.         cmp    cl, cr            ;See if carriage return
  51.         je    NoSuchParm
  52. ;
  53. ; We just encountered an argument, is it the one we want?
  54. ;
  55.         dec    ax
  56.         jz    GrabThisOne
  57. ;
  58. ; If this isn't the argument we want, skip it.
  59. ;
  60.         cmp    cl, '"'            ;See if it's a string.
  61.         je    GotString
  62.         cmp    cl, "'"
  63.         je    GotString
  64. ;
  65. ; If not a string, skip to next space or CR.
  66. ;
  67. SkipWord:    inc    si
  68.         cmp    byte ptr [si], ' '
  69.         je    CntLoop
  70.         cmp    byte ptr [si], cr
  71.         je    NoSuchParm
  72.         jmp    skipWord
  73. ;
  74. ; If we've got a string, skip to the delimiter or to the end of the line.
  75. ;
  76. GotString:    inc    si
  77.         cmp    cl, [si]        ;See if the delimiter
  78.         je    CntLoop
  79.         cmp    byte ptr [si], cr    ;See if EOLN
  80.         jne    GotString
  81.         jmp    NoSuchParm
  82. ;
  83. ; If the argument counter just went to zero, return the specified string.
  84. ;
  85. GrabThisOne:    cmp    cl, "'"            ;Special case for strings
  86.         je    GetAString
  87.         cmp    cl, '"'
  88.         je    GetAString
  89. ;
  90. ; This is not a parameter surrounded by quotes or apostrophes.  Deal with that
  91. ; here.
  92. ;
  93. ; First, compute the length of this guy-
  94. ;
  95.         push    ds
  96.         push    si
  97.         mov    cx, 0
  98. CntChars:    inc    cx
  99.         inc    si
  100.         cmp    byte ptr [si], ' '
  101.         je    EndOfParm
  102.         cmp    byte ptr [si], cr
  103.         jne    CntChars
  104. ;
  105. EndOfParm:    pop    si
  106.         pop    ds
  107. ;
  108. ; Okay, allocate storage for the new string
  109. ;
  110. CopyString:    inc    cx            ;Don't forget zero byte!
  111.         push    cx
  112.         call    sl_malloc
  113.         pop    cx
  114.         jc    ArgvDone        ;Return if error.
  115.         push    es
  116.         push    di
  117.     rep    movsb                ;Copy the string
  118.         mov    byte ptr es:[di-1], 0    ;Put in zero terminating byte.
  119.         pop    di
  120.         pop    es
  121.         clc                ;Return w/no error.
  122.         jmp    ArgvDone
  123. ;
  124. ;
  125. ; If the parameter is a string surrounded by " or ' then process that down
  126. ; here
  127. ;
  128. GetAString:    mov    al, cl            ;Save delimeter.
  129.         push    ds
  130.         push    si
  131.         mov    cx, -1            ;Don't count quote char
  132. CntChars2:    inc    cx
  133.         inc    si
  134.         cmp    al, [si]
  135.         je    EndOfStr
  136.         cmp    byte ptr [si], cr
  137.         jne    CntChars2
  138. ;
  139. EndOfStr:    pop    si
  140.         pop    ds
  141.         inc    si            ;Skip delimeter
  142.         jmp    CopyString
  143. ;
  144. ; If the user selected a phantom parameter, return a pointer to an
  145. ; empty string:
  146. ;
  147. NoSuchParm:    mov    di, seg StdGrp:EmptyParm
  148.         mov    es, di
  149.         mov    di, offset StdGrp:EmptyParm
  150. ;
  151. ; Come down here when we're done:
  152. ;
  153. ArgvDone:    pop    cx
  154.         pop    ax
  155.         pop    si
  156.         pop    ds
  157.         ret
  158. sl_Argv        endp
  159. ;
  160. EmptyParm    db    0,0,0
  161. ;
  162. stdlib        ends
  163.         end
  164.